using JT808Protocol.Common;
using JT808Protocol.Models.Attributes;
using System;
using System.Text.Json.Serialization;

namespace JT808Protocol.Models.BodyParserStrategy.Location.LocationAddition
{
    /// <summary>
    /// 异常驾驶
    /// </summary>
    public class LaAbnormalDriving_0x18 : ILocationAddition
    {
        /// <summary>
        /// 报警状态，0 疲劳，1 打开话，2 抽烟，3 -10 保留，11-15自定义
        /// </summary>
        [JsonPropertyName("报警状态")] public ushort AlarmStatus { get; set; }

        #region 报警详情

        /// <summary>
        /// 疲劳
        /// </summary>
        [JsonPropertyName("疲劳")]
        [JsonBooleanString]
        public bool IsTire { get; set; }

        /// <summary>
        /// 打电话
        /// </summary>
        [JsonPropertyName("打电话")]
        [JsonBooleanString]
        public bool IsRingUp { get; set; }

        /// <summary>
        /// 抽类图
        /// </summary>
        [JsonPropertyName("抽类图")]
        [JsonBooleanString]
        public bool IsSmoking { get; set; }

        #endregion 报警详情

        /// <summary>
        /// 疲劳程度（0-100）
        /// </summary>
        [JsonPropertyName("疲劳程度（0-100）")] public byte TireDegree { get; set; }

        public override ILocationAddition ReadEntity(ReadOnlySpan<byte> source, int startIndex)
        {
            DataLength = (byte)(source[startIndex + 1] + 2);
            if (DataLength != 3 + 2)
            {
                //Logger.Warn($"位置附加信息的异常驾驶长度错误，略过解析");
                return null;
            }

            AlarmStatus = ParseHelper.Uint16(ref source, startIndex + 2);

            TireDegree = source[startIndex + 4];

            IsTire = (source[startIndex + 2] & 0x01) == 0x01;

            IsRingUp = (source[startIndex + 2] & 0x02) == 0x02;

            IsSmoking = (source[startIndex + 2] & 0x04) == 0x04;

            return this;
        }
    }
}